#!/usr/bin/perl -w
use strict;
use WWW::Mechanize;

################## Global Variables #####################
my $payor_url = 'http://10.10.0.4/pickupreport/report.html';
my $score_url = 'http://10.10.0.4/pickupreport/scoreport.html';
#my $DIR = '/home/t0612z/workspace/etrack_srv6';
my $DIR = '/opt/medlytix/WWW/dboard/app/views/tvtrack';
my $date = undef;

################## Subrountines #########################
sub get_unpickup_part($$)
{
    my $url = shift;
    my $file = shift;
    my $title = undef;
    
    $file =~ /^(.*)\.html/ || die "Html file name is not correct!";
    
    if($1 eq 'report') {
        $title = "Payorintel";
    }
    else {
        $title = "Score";
    }
     
    
    my $mech = WWW::Mechanize->new();
    $mech->get( $url ) || die "Can't fetch the html page!";
    my $content = $mech->content();
   
    $content =~ /Generated\s*at:\s*(.*?)<\/p>/s;
    $date = $1;
    
    $content =~ s/^.*?<tr class="alternaterow">//s;
    $content = "<tr class=\"alternaterow\">".$content;
    $content =~ s/<\/table>.*<h4>.*//s 
        || die "Removing subtables failed!";

 

    $content =~ s/<tr[^<]*\s*<td[^<]*\s*<p[^<]*\s*<a[^<]*\s*<\/a>\s*<\/p>\s*<\/td>\s*(<td>\&nbsp<\/td>\s*){10}\s+<\/tr>//sg;
    $content =~ s/^\s*//s;
    $content =~ s/\s*$//s;
    $content =~ s/class="alternaterow"//s;
    $content =~ s/class="content"><p><a\s*href="[^>]*>/class="date" style="font-weight: bold">/gs;
    $content =~ s/bgcolor="66FF66"><a href="[^>]*>/style="font-weight: bold; text-align: center; color:#669; font-size:14px;" >/gs;
    $content =~ s/<\/a>//gs;
    $content =~ s/<\/p>//gs;
    
    make_file_head($file);
    open FL, ">>$DIR/$file" || die "Open $file failed!";
    print FL $content;
    close FL;
    make_file_tail($file);
}

sub make_file_head($)
{
    my $file = shift;
    my $title = undef;
    
    $file =~ /^(.*)\.html/ || die "Html file name is not correct!";
    
    if($1 eq 'report') {
        $title = "Payorintel";
    }
    else {
        $title = "Score";
    }
    
    open FL, ">$DIR/$file" || die "Open $file failed!";
    print FL "<head>\n";
    print FL "<title>$title Pickup Report</title>\n";
    print FL "<%= stylesheet_link_tag \"newinprocess\"%>\n";
    print FL "</head>\n";
    print FL "<body>\n";
    print FL "<div id=\"page\">\n";
    print FL "<div style=\"margin-top:100px; margin-left: 20px\">\n";
    print FL "<h4>$title Pickup Report</h4><span class=\"footage\">Generated at: $date</span>\n";
    print FL "<br><br><br>\n";
    print FL "<table class=\"stats\" cellspacing=\"0\">\n";
    print FL "<thead><tr><th class=\"date\">Client Name</th>\n";
    print FL "<th class=\"date\" colspan=\"10\">Files Not Picked Up</th></tr></thead>\n";
    close FL;
}


sub make_file_tail($)
{
    my $file = shift;
   
    
    #print $file;

    open FL, ">>$DIR/$file" || die "Open $file failed!";
    print FL "</table>\n";
    print FL "</div>\n";
    print FL "</body>\n";
    print FL "</html>\n";
    close FL;
}

########################## Main Part #####################

get_unpickup_part($payor_url,"report.html");
get_unpickup_part($score_url,"scoreport.html");
exit 0;